package com.mt.mtyx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.common.utils.Helper;
import com.mt.mtyx.entity.product.Category;
import com.mt.mtyx.product.service.CategoryService;
import com.mt.mtyx.vo.product.CategoryQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WMT
 * @Date 2023/7/20 10:34
 */
@Api(tags = "商品分类")
@RequestMapping("/admin/product/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @ApiOperation("分页条件查询商品分类列表")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<Category>> selectAll(
            @PathVariable Long page,
            @PathVariable Long limit,
            CategoryQueryVo queryVo
    ) throws Exception {
        return Result.success(categoryService.selectAll(new Page<Category>(page, limit), queryVo));
    }

    @ApiOperation("获取商品分类详情")
    @GetMapping("/get/{id}")
    public Result<Category> getDetails(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(categoryService.getDetails(id));
    }


    @ApiOperation("新增商品分类")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Category category) {
        if (StringUtils.isEmpty(category.getName())) {
            throw new BusinessException("商品分类名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (categoryService.existsByName(category.getName())) {
            throw new BusinessException("商品分类名称已存在！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(categoryService.insert(category));
    }


    @ApiOperation("删除商品分类")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(categoryService.delete(id));
    }

    @ApiOperation("批量删除商品分类")
    @DeleteMapping("/batchRemove")
    public Result<Integer> batchRemove(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(categoryService.batchDelete(ids));
    }


    @ApiOperation("修改商品分类")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Category category) {
        if (StringUtils.isEmpty(category.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(category.getName())) {
            throw new BusinessException("商品分类名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        Category categoryDb = categoryService.getDetails(category.getId());
        if (categoryDb != null) {
            if (!categoryDb.getName().equals(category.getName())) {
                if (categoryService.existsByName(category.getName())) {
                    throw new BusinessException("商品分类名称已存在！", HttpStatus.BAD_REQUEST.value());
                }
            }
        }
        return Result.success(categoryService.update(category));
    }


    @ApiOperation("获取全部商品分类")
    @GetMapping("/findAllList")
    public Result<List<Category>> getAll() {
        return Result.success(categoryService.findAllList());
    }
}
